package com.local.web.controller;

import com.local.common.constants.BusinessException;
import com.local.common.constants.ExceptionCodeEnum;
import com.local.common.util.MD5Util;
import com.local.common.util.ResultUtil;
import com.local.user.pojo.User;
import com.local.user.pojo.UserLoginRequestParam;
import com.local.web.constants.CaptchaTypeEnum;
import com.local.web.constants.WebConstants;
import com.local.web.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Objects;

/**
 * 用户
 */

@Controller
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 登录页面
     * @return
     */
    @GetMapping({"/login", "/login.html"})
    public String login() {
        return "mall/login";
    }


    /**
     * 登录操作
     * @param session
     * @param loginName
     * @param password
     * @return
     */
    @PostMapping("/login")
    @ResponseBody
    public ResultUtil login(HttpSession session, @RequestParam("loginName") String loginName,
                            @RequestParam("password") String password,
                            @RequestParam("captchaCode") String captchaCode) {
        if (Objects.isNull(loginName) || loginName.isEmpty()
                || Objects.isNull(password) || password.isEmpty()
                || Objects.isNull(captchaCode) || captchaCode.isEmpty()) {
            return new ResultUtil(201, "请完整输入手机号、密码和验证码");
        }

        String captchaCodeSession = session.getAttribute(CaptchaTypeEnum.LOGIN_CAPTCHA.getValue()).toString();
        if (!captchaCode.equalsIgnoreCase(captchaCodeSession)) {
            return new ResultUtil(201, "验证码错误，请重新输入验证码");
        }

        UserLoginRequestParam requestParam = new UserLoginRequestParam(loginName, MD5Util.MD5Encode(password, WebConstants.UTF_ENCODING));
        User user = userService.getByLoginNamePassword(requestParam, session);
        if (user.getLockedFlag() == 1) {
            return new ResultUtil(201, "该账户已被锁定，无法登录");
        } else if (user.getIsDeleted() == 1) {
            return new ResultUtil(201, "该账户已被删除，无法登录");
        }

        return new ResultUtil(200, "登录成功");
    }


    @GetMapping("/logout")
    public String logout(HttpSession session) {
        session.removeAttribute(WebConstants.WEB_USER_SESSION_KEY);
        session.removeAttribute(WebConstants.WEB_USER_CART_SESSION_KEY);
        return "redirect:/";
    }


    /**
     * 在订单核对页面中，修改地址
     * @param user
     * @return
     */
    @PostMapping("/user/updateAddress")
    @ResponseBody
    public ResultUtil updateAddress(@RequestBody User user, HttpSession session) {
        ResultUtil result = userService.updateAddress(user);
        if (Objects.isNull(result)) {
            throw new BusinessException(ExceptionCodeEnum.BUSINESS_EXCEPTION.getCode(), ExceptionCodeEnum.BUSINESS_EXCEPTION.getMessage());
        }

        if (result.getCode() == 200) {
            //更新成功，则同时把session中的user也更新了
            User userSession = (User) session.getAttribute(WebConstants.WEB_USER_SESSION_KEY);
            userSession.setAddress(user.getAddress());
            session.removeAttribute(WebConstants.WEB_USER_SESSION_KEY);
            session.setAttribute(WebConstants.WEB_USER_SESSION_KEY, userSession);
        }

        return result;
    }
}
